package zhang.lao.api.service;/**
 * Created by zhangqin on 2017/10/23.
 */

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhang.lao.build.tool.CoordinateConversion;
import zhang.lao.build.tool.ListUtils;
import zhang.lao.build.tool.Point;
import zhang.lao.build.tool.UUIDTool;
import zhang.lao.build.tool.date.DateStyle;
import zhang.lao.build.tool.date.DateUtil;
import zhang.lao.dao.base.*;
import zhang.lao.dao.my.WnDeviceGt06LocationDao;
import zhang.lao.extents.spring.exception.ApiException;
import zhang.lao.pojo.ApiGt06PointReq;
import zhang.lao.pojo.ApiGt06PointResp;
import zhang.lao.pojo.api.req.*;
import zhang.lao.pojo.api.resp.*;
import zhang.lao.build.mybatis.jdbc.auto.model.*;
import zhang.lao.service.console.MachineService;
import zhang.lao.service.search.SearchService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author
 * @create 2017-10-23 15:38
 **/

@Service
public class FarmService {
    @Autowired
    private WnFarmlandDao wnFarmlandDao;

    @Autowired
    private WnAppCompanyFarmDao wnAppCompanyFarmDao;
    @Autowired
    private WnMachineDao wnMachineDao;
    @Autowired
    private WnFarmDao wnFarmDao;
    @Autowired
    private WnDeviceGt06LocationDao wnDeviceGt06LocationDao;
    @Autowired
    private WnCompanyDao wnCompanyDao;
    @Autowired
    private WnMachineDeviceDao wnMachineDeviceDao;
    @Autowired
    private MachineService machineService;
    @Autowired
    private SearchService searchService;
    @Autowired
    private WnAppUserDao wnAppUserDao;
    @Autowired
    private WnMachineDeviceActiveGt06Dao wnMachineDeviceActiveGt06Dao;

    public ApiRespData<List<ApiFarmListResp>> getFarms(ApiReqData<ApiReqBody> apiReqBodyApiReqData){
        List<ApiFarmListResp> listResps = Lists.newArrayList();
        String account = apiReqBodyApiReqData.getHead().getAccount();
        WnAppCompanyFarmExample wnAppCompanyFarmExample = new WnAppCompanyFarmExample();
        wnAppCompanyFarmExample.createCriteria().andAppKeyEqualTo(account);
        List<WnAppCompanyFarm> wnAppCompanyFarms = wnAppCompanyFarmDao.selectByExample(wnAppCompanyFarmExample);
        if(wnAppCompanyFarms!=null){
            for (WnAppCompanyFarm wnAppCompanyFarm : wnAppCompanyFarms) {
                ApiFarmListResp apiFarmListResp = new ApiFarmListResp();
                WnFarm wnFarm = wnFarmDao.selectByPrimaryKey(wnAppCompanyFarm.getFarmId());
                apiFarmListResp.setCompanyId(wnFarm.getCompanyId());
                WnCompany wnCompany = wnCompanyDao.selectByPrimaryKey(wnFarm.getCompanyId());
                apiFarmListResp.setCompanyName(wnCompany.getName());
                apiFarmListResp.setFarmName(wnFarm.getName());
                apiFarmListResp.setCompanyLat(wnCompany.getLat());
                apiFarmListResp.setCompanyLng(wnCompany.getLng());
                apiFarmListResp.setFarmLat(wnFarm.getLat());
                apiFarmListResp.setFarmLng(wnFarm.getLng());
                apiFarmListResp.setFarmId(wnFarm.getId());
                listResps.add(apiFarmListResp);
            }
        }
        return ApiRespData.buildSucc(listResps);
    }


    public ApiRespData<List<ApiDeviceList>> getDevices(ApiReqData<ApiDeviceListReq> apiDeviceListReqApiReqData){
        ApiDeviceListReq req = apiDeviceListReqApiReqData.getBody();
        if(req!=null&&StringUtils.isNotBlank(req.getFarmIds())){
            List<String> farmIds  = Lists.newArrayList(req.getFarmIds().split(","));
            List<ApiDeviceList> newGt06Location = wnDeviceGt06LocationDao.findNewGt06Location(farmIds);
            if(ListUtils.isNotEmpty(newGt06Location)){
                for (ApiDeviceList apiDeviceList : newGt06Location) {
                    WnMachineAppUser wnMachineAppUser = machineService.getMachineAppUser(apiDeviceList.getMachineId());

                    if(StringUtils.isNotBlank(wnMachineAppUser.getAppUserId())){
                        WnAppUser wnAppUser = wnAppUserDao.selectByPrimaryKey(wnMachineAppUser.getAppUserId());
                        if(wnAppUser!=null){
                            apiDeviceList.setUserPhoneNumber(wnAppUser.getPhoneNumber());
                            apiDeviceList.setUserName(wnAppUser.getNickName());
                        }
                    }
                }
            }
            return ApiRespData.buildSucc(newGt06Location);
        }else {
            List<String> farmIds = Lists.newArrayList();
            String account = apiDeviceListReqApiReqData.getHead().getAccount();
            WnAppCompanyFarmExample wnAppCompanyFarmExample = new WnAppCompanyFarmExample();
            wnAppCompanyFarmExample.createCriteria().andAppKeyEqualTo(account);
            List<WnAppCompanyFarm> wnAppCompanyFarms = wnAppCompanyFarmDao.selectByExample(wnAppCompanyFarmExample);
            if (wnAppCompanyFarms != null) {
                for (WnAppCompanyFarm wnAppCompanyFarm : wnAppCompanyFarms) {
                    farmIds.add(wnAppCompanyFarm.getFarmId());
                }
                if(farmIds.size()>0){
                    List<ApiDeviceList> newGt06Location = wnDeviceGt06LocationDao.findNewGt06Location(farmIds);
                    if(ListUtils.isNotEmpty(newGt06Location)){
                        for (ApiDeviceList apiDeviceList : newGt06Location) {
                            WnMachineAppUser wnMachineAppUser = machineService.getMachineAppUser(apiDeviceList.getMachineId());

                            if(StringUtils.isNotBlank(wnMachineAppUser.getAppUserId())){
                                WnAppUser wnAppUser = wnAppUserDao.selectByPrimaryKey(wnMachineAppUser.getAppUserId());
                                if(wnAppUser!=null){
                                    apiDeviceList.setUserPhoneNumber(wnAppUser.getPhoneNumber());
                                    apiDeviceList.setUserName(wnAppUser.getNickName());
                                }
                            }
                        }
                    }

                    return ApiRespData.buildSucc(newGt06Location);
                }
            }
        }
        return ApiRespData.buildFail(ApiResultEnum.NODATE);
    }


    public ApiRespData<List<ApiUserOverviewResp>> userOverview(ApiReqData<ApiReqBody> apiDeviceListReqApiReqData) {
        List<ApiUserOverviewResp> list = Lists.newArrayList();
        WnCompanyExample wnCompanyExample = new WnCompanyExample();
        wnCompanyExample.createCriteria().andTypeEqualTo(1);
        List<WnCompany> wnCompanies = wnCompanyDao.selectByExample(wnCompanyExample);
        if(wnCompanies!=null){
            for (WnCompany wnCompany : wnCompanies) {
                ApiUserOverviewResp apiUserOverviewResp = new ApiUserOverviewResp();
                apiUserOverviewResp.setCompanyLat(wnCompany.getLat());
                apiUserOverviewResp.setCompanyLng(wnCompany.getLng());
                apiUserOverviewResp.setCompanyName(wnCompany.getName());
                WnFarmExample wnFarmExample = new WnFarmExample();
                wnFarmExample.createCriteria().andCompanyIdEqualTo(wnCompany.getId());
                apiUserOverviewResp.setFramCount(wnFarmDao.countByExample(wnFarmExample));

                List<String> listFarmIds = wnFarmDao.getListFarmIds(wnCompany.getId());
                WnMachineExample wnMachineExample = new WnMachineExample();
                wnMachineExample.createCriteria().andFarmIdIn(listFarmIds);
                apiUserOverviewResp.setMachineCount(wnMachineDao.countByExample(wnMachineExample));
                list.add(apiUserOverviewResp);
            }
        }
        return ApiRespData.buildSucc(list);
    }


   public ApiRespData<List<ApiGt06PointResp>>  gt06PointReq(ApiReqData<ApiGt06PointReq> pointGt06ReqApiReqData){
       ApiGt06PointReq body = pointGt06ReqApiReqData.getBody();
       if(StringUtils.isBlank(body.getDeviceNumber())||StringUtils.isBlank(body.getStartTime())||StringUtils.isBlank(body.getEndTime())){
           throw new ApiException(ApiResultEnum.PARAMEERROR);
       }else{
           try {
               List<ApiGt06PointResp> list =  searchService.gt06Resp(body);
               if(ListUtils.isNotEmpty(list)){
                   for (ApiGt06PointResp apiGt06PointResp : list) {
                       Point point = CoordinateConversion.wgs_gcj_encrypts(apiGt06PointResp.getLat(), apiGt06PointResp.getLng());
                       apiGt06PointResp.setLat(point.getLat());
                       apiGt06PointResp.setLng(point.getLng());
                   }
               }
          return ApiRespData.buildSucc(list);
           } catch (Exception e) {
               throw new ApiException(ApiResultEnum.ERR500);
           }
       }
   }


    public ApiRespData<List<ApiWnFarmlandListResp>>  apiWnFarmlandListResp(ApiReqData<ApiWnFarmlandListReq> pointGt06ReqApiReqData){
        ApiWnFarmlandListReq body = pointGt06ReqApiReqData.getBody();
        WnFarmlandExample wnFarmlandExample = new WnFarmlandExample();
        List<ApiWnFarmlandListResp> listResps = Lists.newArrayList();
       wnFarmlandExample.createCriteria().andFarmIdIn(Lists.newArrayList(body.getFarmIds().split(",")));
        List<WnFarmland> wnFarmlands = wnFarmlandDao.selectByExample(wnFarmlandExample);
        if(wnFarmlands!=null){
            for (WnFarmland wnFarmland : wnFarmlands) {
                ApiWnFarmlandListResp apiWnFarmlandListResp = new ApiWnFarmlandListResp();
                BeanUtils.copyProperties(wnFarmland,apiWnFarmlandListResp);
                listResps.add(apiWnFarmlandListResp);
            }
        }
        return ApiRespData.buildSucc(listResps);
    }

    public ApiRespData<ApiRespBody>  apiWnFarmlandAddResp(ApiReqData<ApiWnFarmlandAddReq> pointGt06ReqApiReqData){
        ApiReqHead head = pointGt06ReqApiReqData.getHead();
        String account = head.getAccount();
        ApiWnFarmlandAddReq body = pointGt06ReqApiReqData.getBody();
        WnFarmland wnFarmland = new WnFarmland();
        BeanUtils.copyProperties(body,wnFarmland);
        wnFarmland.setCreateTime(new Date());
        wnFarmland.setCreateUser(account);
        wnFarmland.setId(UUIDTool.getUUID());
        wnFarmlandDao.insertSelective(wnFarmland);
        return ApiRespData.buildSucc();
    }

    public ApiRespData<List<ApiDeviceActiceDatesResp>>  apiDeviceActiceDatesResp(ApiReqData<ApiDeviceActiceDatesReq> apiDeviceActiceDatesReqApiReqData){
        WnMachineDeviceExample wnMachineDeviceExample = new WnMachineDeviceExample();
        ApiDeviceActiceDatesReq body = apiDeviceActiceDatesReqApiReqData.getBody();
        wnMachineDeviceExample.createCriteria().andDeviceNumberEqualTo(body.getDeviceNumber());
        WnMachineDevice wnMachineDevice = ListUtils.getFirst(wnMachineDeviceDao.selectByExample(wnMachineDeviceExample));
        List<ApiDeviceActiceDatesResp> list = Lists.newArrayList();
        if (wnMachineDevice == null) {
            return ApiRespData.buildSucc(list);
        }
        WnMachineDeviceActiveGt06Example wnMachineDeviceActiveGt06Example = new WnMachineDeviceActiveGt06Example();
        wnMachineDeviceActiveGt06Example.createCriteria().andDeviceIdEqualTo(wnMachineDevice.getId());
        wnMachineDeviceActiveGt06Example.setOrderByClause("create_time desc");
        List<WnMachineDeviceActiveGt06> wnMachineDeviceActiveGt06s = wnMachineDeviceActiveGt06Dao.selectByExample(wnMachineDeviceActiveGt06Example);
        if(wnMachineDeviceActiveGt06s!=null){
            for (WnMachineDeviceActiveGt06 wnMachineDeviceActiveGt06 : wnMachineDeviceActiveGt06s) {
                ApiDeviceActiceDatesResp apiDeviceActiceDatesResp = new ApiDeviceActiceDatesResp();
                apiDeviceActiceDatesResp.setWorkDate(DateUtil.DateToString(wnMachineDeviceActiveGt06.getCreateDate(), DateStyle.YYYY_MM_DD));
                list.add(apiDeviceActiceDatesResp);
            }
        }
        return ApiRespData.buildSucc(list);
    }


}
